package org.jet.emall.prod.controller;

import org.jet.emall.common.restful.result.CommonPage;
import org.jet.emall.common.restful.result.CommonResult;
import org.jet.emall.prod.dto.CartDTO;
import org.jet.emall.prod.dto.CategoryDTO;
import org.jet.emall.prod.dto.SkuDTO;
import org.jet.emall.prod.dto.SpuDetailsDTO;
import org.jet.emall.prod.service.CustomProductService;
import org.jet.emall.prod.transfer.CategoryVoTransfer;
import org.jet.emall.prod.transfer.SkuVoTransfer;
import org.jet.emall.prod.vo.CartVO;
import org.jet.emall.prod.vo.CategoryVO;
import org.jet.emall.prod.vo.SkuVO;
import org.jet.emall.prod.vo.SpuDetailsVO;
import org.jet.emall.prod.wrapper.CartIdAndCartItemIdsWrapper;
import org.jet.emall.prod.wrapper.CartIdAndSkuIdAndCountWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-25 11:59
 */
@RestController
public class ProductController{

    @Autowired
    private CustomProductService customProductService;


    @RequestMapping(value = "/skus",method = RequestMethod.GET)
    public CommonResult<CommonPage<SkuVO>> page(String query, Integer pageNum, Integer pageSize){
        CommonPage<SkuDTO> page = customProductService.page(query,pageNum,pageSize);
        ((CommonPage<SkuVO>)(Object)page).setList( SkuVoTransfer.INSTANCE.toVoList(page.getList()) );
        return CommonResult.success((CommonPage<SkuVO>)(Object)page,"分页查找成功");
    }

    @RequestMapping(value = "/sku-page",method = RequestMethod.GET)
    public CommonResult<CommonPage<SkuVO>> page(Long cateId, Long brandId,  String query, Integer pageNum, Integer pageSize){

        CommonPage<SkuDTO> page = customProductService.page2(cateId,brandId,query,pageNum,pageSize);
        ((CommonPage<SkuVO>)(Object)page).setList( SkuVoTransfer.INSTANCE.toVoList(page.getList()) );
        return CommonResult.success((CommonPage<SkuVO>)(Object)page,"分页查找成功");
    }


    @RequestMapping(value = "/categories",method = RequestMethod.GET)
    public CommonResult<List<CategoryVO>> getProductCategories(){
        List<CategoryDTO> dtoList = customProductService.getProductCategories();
        List<CategoryVO> voList = CategoryVoTransfer.INSTANCE.toVoList(dtoList);
        return CommonResult.success(voList,"获取商品分类成功");
    }


    @RequestMapping(value = "/spudetails/{id}",method = RequestMethod.GET)
    public CommonResult<SpuDetailsVO> getSpuDetails(@PathVariable Long id){
        SpuDetailsDTO dto = customProductService.getSpuDetails(id);
        CommonResult<SpuDetailsDTO> result = CommonResult.success(dto, "获取商品详情成功");
        return (CommonResult<SpuDetailsVO>)(Object)result;
    }


    @RequestMapping(value = "/skudetails/{id}",method = RequestMethod.GET)
    public CommonResult<SpuDetailsVO> getSpuDetailsBySkuId(@PathVariable Long id){
        SpuDetailsDTO dto = customProductService.getSpuDetailsBySkuId(id);
        CommonResult<SpuDetailsDTO> result = CommonResult.success(dto, "获取商品详情成功");
        return (CommonResult<SpuDetailsVO>)(Object)result;
    }


    @RequestMapping(value = "/cart/{customerId}",method = RequestMethod.GET)
    public CommonResult<CartVO> getCart(@PathVariable Long customerId){
        CartDTO dto = customProductService.getCart(customerId);
        return (CommonResult<CartVO>)(Object)CommonResult.success(dto,"获取购物车成功");
    }


    @RequestMapping(value = "/cart",method = RequestMethod.PATCH)
    public CommonResult<CartVO> updateCart(@RequestBody @Valid CartIdAndSkuIdAndCountWrapper wrapper){
        CartDTO dto = customProductService.updateCart(wrapper.getCartId(), wrapper.getSkuId(), wrapper.getCount());
        return (CommonResult<CartVO>)(Object)CommonResult.success(dto,"更新购物车成功");
    }



    @RequestMapping(value = "/wx/cart",method = RequestMethod.PUT)
    public CommonResult<CartVO> wxUpdateCart(@RequestBody @Valid CartIdAndSkuIdAndCountWrapper wrapper){
        CartDTO dto = customProductService.updateCart(wrapper.getCartId(), wrapper.getSkuId(), wrapper.getCount());
        return (CommonResult<CartVO>)(Object)CommonResult.success(dto,"更新购物车成功");
    }


    @RequestMapping(value = "/cart/items",method = RequestMethod.DELETE)
    public CommonResult<CartVO> deleteCartItems(@RequestBody @Valid CartIdAndCartItemIdsWrapper wrapper){
        CartDTO dto = customProductService.deleteCartItems(wrapper.getCartId(),wrapper.getCartItemIds());
        return (CommonResult<CartVO>)(Object)CommonResult.success(dto,"删除购物项成功");
    }


    @RequestMapping(value = "/cart/items",method = RequestMethod.PATCH)
    public CommonResult<CartVO> updateCartItemsSelected(@RequestBody @Valid CartIdAndCartItemIdsWrapper wrapper){
        CartDTO dto = customProductService.updateCartItemsSelected(wrapper.getCartId(),wrapper.getCartItemIds());
        return (CommonResult<CartVO>)(Object)CommonResult.success(dto,"选择购物项成功");
    }

    @RequestMapping(value = "/wx/cart/items",method = RequestMethod.PUT)
    public CommonResult<CartVO> wxUpdateCartItemsSelected(@RequestBody @Valid CartIdAndCartItemIdsWrapper wrapper){
        CartDTO dto = customProductService.updateCartItemsSelected(wrapper.getCartId(),wrapper.getCartItemIds());
        return (CommonResult<CartVO>)(Object)CommonResult.success(dto,"选择购物项成功");
    }


}
